<?php
declare (strict_types=1);

namespace app\exam\model;

use app\common\model\{BaseModel, ProductType};
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;

/**
 * 公开考试试卷关联模型
 */
class Examination extends BaseModel
{
    use softDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'exam_module_examination';

    /**
     * 定义字段类型
     *
     * @var [type]
     */
    protected $type = [
        'module_id' => 'integer',
        'paper_id'  => 'integer',

    ];

    /**
     * 定义隐藏字段
     *
     * @var [type]
     */
    protected $hidden = [
        'create_time',
        'update_time',
        'delete_time',
    ];

    /**
     * 全局查询
     *
     * @var array
     */
    protected $globalScope = ['status'];

    /**
     * 添加器
     *
     * @var array
     */
    protected $append = ['price', 'user_price']; // 根据用户身份计算的价格

    /**
     * 限制状态
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-02-22
     *
     * @param  [type] $query [description]
     * @return void [type]        [description]
     */
    public function scopeStatus($query)
    {
        $query->where('status', 1);
    }

    /**
     * 返回价格
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date     21/03/29
     *
     * @param string $value
     * @param        $data
     * @return float
     */
    public function getPriceAttr($value = '', $data): float
    {
        // 考试配置
        $config = app(PaperConfig::class)->where('data_key', 'examination_' . $data['module_id'])->where('paper_id', $data['paper_id'])->field('cnf_data')->findOrEmpty();
        return $config['cnf_data']['price'] ?? 0;
    }

    /**
     * 根据用户身份返回价格
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date     21/03/29
     *
     * @param string $value
     * @param        $data
     * @return float
     */
    public function getUserPriceAttr($value = '', $data): float
    {
        // 考试配置
        $config        = app(PaperConfig::class)->where('data_key', 'examination_' . $data['module_id'])->where('paper_id', $data['paper_id'])->field('cnf_data')->findOrEmpty();
        $data['price'] = $config['cnf_data']['price'] ?? 0;
        return self::getUserPrice($data);
    }

    /**
     * 关联试卷
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-01
     *
     * @return BelongsTo [type] [description]
     */
    public function paper()
    {
        return $this->belongsTo(Paper::class, 'paper_id');
    }

    /**
     * 关联试卷机构
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-01
     *
     * @return BelongsTo [type] [description]
     */
    public function paperMhm()
    {
        return $this->belongsTo(Paper::class, 'paper_id')->bind(['mhm_id']);
    }

    /**
     * 关联试卷名称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-01
     *
     * @return BelongsTo [type] [description]
     */
    public function paperTitle()
    {
        return $this->belongsTo(Paper::class, 'paper_id')->bind(['title']);
    }

    /**
     * 关联试卷
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-01
     *
     * @return BelongsTo [type] [description]
     */
    public function paperBase()
    {
        return $this->belongsTo(Paper::class, 'paper_id')
            ->field('id,title,start_time,end_time,exam_number,total_count')
            // ->append(['exam_status'])
            ->bind(['title', 'total_count', 'start_time', 'end_time', 'exam_number']);//'exam_status'
    }

    /**
     * 关联考试记录次数
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-06
     *
     * @return mixed [type] [description]
     */
    public function paperAnswer()
    {
        return $this->hasMany(PaperAnswer::class, 'paper_id', 'paper_id')
            ->where('user_id', request()->mid)->whereRollupId(0)->whereCourseId(0);
    }

    /**
     * 购买人数
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-03-31
     *
     * @return mixed [type] [description]
     */
    public function boughtNum()
    {
        return $this
            ->hasMany(OrderItem::class, 'product_id')
            ->whereProductTypeId(ProductType::getProductIdByKey('exam'))
            ->join('payment_order po', "po.order_no=" . env('database.prefix', '') . "payment_order_item.order_no")
            ->where('status', 'IN', '20,30,40');
    }

    /**
     * 试卷是否购买
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-03-30
     */
    public function HasBought()
    {
        return $this->hasOne(OrderItem::class, 'product_id')
            ->whereProductTypeId(ProductType::getProductIdByKey('exam'))
            ->join('payment_order po', "po.order_no=" . env('database.prefix', '') . "payment_order_item.order_no AND po.user_id=" . request()->mid)
            ->where('status', 'IN', '20,30,40');
    }

}
